<template>
  <div class="app-container">
    <t-form
      :data="queryParams"
      ref="queryRef"
      layout="inline"
      v-show="showSearch"
      label-width="68px"
      class="mb20"
    >
      <t-form-item label="系统模块" prop="title">
        <t-input
          v-model="queryParams.title"
          placeholder="请输入系统模块"
          clearable
          style="width: 240px"
          @enter="handleQuery"
        />
      </t-form-item>
      <t-form-item label="操作人员" prop="operName">
        <t-input
          v-model="queryParams.operName"
          placeholder="请输入操作人员"
          clearable
          style="width: 240px"
          @enter="handleQuery"
        />
      </t-form-item>
      <t-form-item label="类型" prop="businessType">
        <t-select
          v-model="queryParams.businessType"
          placeholder="操作类型"
          clearable
          style="width: 240px"
        >
          <t-option
            v-for="dict in sys_oper_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </t-select>
      </t-form-item>
      <t-form-item label="状态" prop="status">
        <t-select
          v-model="queryParams.status"
          placeholder="操作状态"
          clearable
          style="width: 240px"
        >
          <t-option
            v-for="dict in sys_common_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </t-select>
      </t-form-item>
      <t-form-item label="操作时间" style="width: 308px">
        <t-date-range-picker
          v-model="dateRange"
          enable-time-picker
          allow-input
          clearable
        />
      </t-form-item>
      <t-form-item>
        <t-space>
          <t-button theme="primary" @click="handleQuery">
            搜索
            <template #icon>
              <t-icon name="search" />
            </template>
          </t-button>
          <t-button theme="default" @click="resetQuery">
            重置
            <template #icon>
              <t-icon name="refresh" />
            </template>
          </t-button>
        </t-space>
      </t-form-item>
    </t-form>

    <t-row :gutter="10" class="mb8">
      <t-col :span="1.5">
        <t-button
          theme="danger"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['monitor:operlog:remove']"
          >删除
          <template #icon>
            <t-icon name="delete" />
          </template>
        </t-button>
      </t-col>
      <t-col :span="1.5">
        <t-button
          theme="danger"
          @click="handleClean"
          v-hasPermi="['monitor:operlog:remove']"
          >清空
          <template #icon>
            <t-icon name="delete" />
          </template>
        </t-button>
      </t-col>
      <t-col :span="1.5">
        <t-button
          theme="warning"
          @click="handleExport"
          v-hasPermi="['monitor:operlog:export']"
          >导出
          <template #icon>
            <t-icon name="download" />
          </template>
        </t-button>
      </t-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </t-row>

    <t-table
      ref="operlogRef"
      v-loading="loading"
      :data="operlogList"
      row-key="operId"
      :columns="columns"
      @select-change="handleSelectionChange"
      :default-sort="defaultSort"
      @sort-change="handleSortChange"
    >
      <template #businessType="scope">
        <dict-tag :options="sys_oper_type" :value="scope.row.businessType" />
      </template>
      <template #status="scope">
        <dict-tag :options="sys_common_status" :value="scope.row.status" />
      </template>
      <template #operTime="scope">
        <span>{{ parseTime(scope.row.operTime) }}</span>
      </template>
      <!-- <template #costTime="scope">
        <span>{{ scope.row.costTime }}毫秒</span>
      </template> -->
      <template #action="scope">
        <t-button
          variant="text"
          theme="primary"
          @click="handleView(scope.row, scope.index)"
          v-hasPermi="['monitor:operlog:query']"
          >详细
          <template #icon>
            <t-icon name="browse" />
          </template>
        </t-button>
      </template>
    </t-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 操作日志详细 -->
    <t-dialog
      title="操作日志详细"
      v-model:visible="open"
      width="700px"
      attach="body"
    >
      <t-form :data="form" label-width="100px">
        <t-row>
          <t-col :span="6">
            <t-form-item label="操作模块："
              >{{ form.title }} / {{ typeFormat(form) }}</t-form-item
            >
            <t-form-item label="登录信息："
              >{{ form.operName }} / {{ form.operIp }} /
              {{ form.operLocation }}</t-form-item
            >
          </t-col>
          <t-col :span="6">
            <t-form-item label="请求地址：">{{ form.operUrl }}</t-form-item>
            <t-form-item label="请求方式：">{{
              form.requestMethod
            }}</t-form-item>
          </t-col>
          <t-col :span="12">
            <t-form-item label="操作方法：">{{ form.method }}</t-form-item>
          </t-col>
          <t-col :span="12">
            <t-form-item label="请求参数：">{{ form.operParam }}</t-form-item>
          </t-col>
          <t-col :span="12">
            <t-form-item label="返回参数：">{{ form.jsonResult }}</t-form-item>
          </t-col>
          <t-col :span="3">
            <t-form-item label="操作状态：">
              <div v-if="form.status === 0">正常</div>
              <div v-else-if="form.status === 1">失败</div>
            </t-form-item>
          </t-col>
          <t-col :span="4">
            <!-- <t-form-item label="消耗时间："
              >{{ form.costTime }}毫秒</t-form-item
            > -->
          </t-col>
          <t-col :span="5">
            <t-form-item label="操作时间：">{{
              parseTime(form.operTime)
            }}</t-form-item>
          </t-col>
          <t-col :span="12">
            <t-form-item label="异常信息：" v-if="form.status === 1">{{
              form.errorMsg
            }}</t-form-item>
          </t-col>
        </t-row>
      </t-form>
      <template #footer>
        <div class="dialog-footer">
          <t-button theme="default" @click="open = false">关 闭</t-button>
        </div>
      </template>
    </t-dialog>
  </div>
</template>

<script setup name="Operlog">
import { list, delOperlog, cleanOperlog } from "@/api/monitor/operlog";

const { proxy } = getCurrentInstance();
const { sys_oper_type, sys_common_status } = proxy.useDict(
  "sys_oper_type",
  "sys_common_status"
);

const operlogList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);
const defaultSort = ref({ sortBy: "operTime", descending: "true" });

const columns = ref([
  { colKey: "row-select", type: "multiple", width: 50, align: "center" },
  { colKey: "operId", title: "日志编号", width: 100, align: "center" },
  {
    colKey: "title",
    title: "系统模块",
    align: "center",
    ellipsis: true,
  },
  {
    colKey: "businessType",
    title: "操作类型",
    align: "center",
    ellipsis: true,
  },
  {
    colKey: "operName",
    title: "操作人员",
    align: "center",
    width: 110,
    ellipsis: true,
    sorter: true,
  },
  {
    colKey: "operIp",
    title: "主机",
    align: "center",
    width: 130,
    ellipsis: true,
  },
  {
    colKey: "operTime",
    title: "操作日期",
    align: "center",
    ellipsis: true,
    sorter: true,
  },
  //   {
  //     colKey: "costTime",
  //     title: "消耗时间",
  //     align: "center",
  //     with: 100,
  //     ellipsis: true,
  //     sorter: true,
  //   },
  {
    colKey: "action",
    title: "操作",
    align: "center",
    width: 100,
    fixed: "right",
  },
]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    title: undefined,
    operName: undefined,
    businessType: undefined,
    status: undefined,
  },
});

const { queryParams, form } = toRefs(data);

/** 查询登录日志 */
function getList() {
  loading.value = true;
  list(proxy.addDateRange(queryParams.value, dateRange.value)).then(
    (response) => {
      operlogList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    }
  );
}
/** 操作日志类型字典翻译 */
function typeFormat(row, column) {
  return proxy.selectDictLabel(sys_oper_type.value, row.businessType);
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  queryParams.orderByColumn = defaultSort.sortBy;
  queryParams.isAsc = defaultSort.descending ? "desc" : "asc";
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selectedRowKeys, { selectedRowData }) {
  ids.value = selectedRowKeys;
  single.value = selectedRowKeys.length != 1;
  multiple.value = !selectedRowKeys.length;
}
/** 排序触发事件 */
function handleSortChange(column, prop, order) {
  queryParams.value.orderByColumn = column.sortBy;
  queryParams.value.isAsc = column.descending ? "desc" : "asc";
  getList();
}
/** 详细按钮操作 */
function handleView(row) {
  open.value = true;
  form.value = row;
}
/** 删除按钮操作 */
function handleDelete(row) {
  const operIds = row.operId || ids.value;
  proxy.$modal
    .confirm('是否确认删除日志编号为"' + operIds + '"的数据项?')
    .then(function () {
      return delOperlog(operIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
/** 清空按钮操作 */
function handleClean() {
  proxy.$modal
    .confirm("是否确认清空所有操作日志数据项?")
    .then(function () {
      return cleanOperlog();
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("清空成功");
    })
    .catch(() => {});
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "monitor/operlog/export",
    {
      ...queryParams.value,
    },
    `config_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
